# Maintainer: Felix Yan <felixonmars@archlinux.org>

pkgname=shadowsocks
pkgver=3.0.0a.20180219
_commit=e332ec93e9c90f1cbee676b022bf2c5d5b7b1239
pkgrel=6
pkgdesc="A lightweight tunnel proxy"
license=('Apache')
url="https://pypi.python.org/pypi/shadowsocks"
arch=('any')
depends=('python-setuptools')
makedepends=('git')
optdepends=('libsodium: For salsa20 and chacha20 support'
            'mbedtls: Optional mbedtls modes')
checkdepends=('bc' 'bind-tools' 'dante' 'libsodium' 'mbedtls' 'procps-ng' 'python-coverage' 'python-nose'
              'python-pycodestyle' 'python-pyflakes' 'python-pysocks')
install=$pkgname.install
source=("$pkgname-$pkgver.tar.gz::https://github.com/shadowsocks/shadowsocks/archive/$_commit.tar.gz"
        pycodestyle.patch
        $pkgname-py3.10-fix-1.patch::https://github.com/shadowsocks/shadowsocks/pull/1489.patch
        $pkgname-py3.10-fix-2.patch::https://github.com/shadowsocks/shadowsocks/pull/1490.patch
        "shadowsocks@.service"
        "shadowsocks-server@.service")
sha512sums=('d3370cedffed385ad9cf1e0390edb535d2d7f949fc79b77875a78cac5bad7b601a5605063e130416a7871a0b7798d4c088c08b2d472570a6429f59d38091e5a7'
            '89d8d984d9f1c20c020e619b8c22315737e1f53e42caf41643232997b7862b92bc3295985b7ddc4a3f3e0c29d962c2f1ee97b8dd6409be82cb93c09d5df1f7ff'
            '08b3f91ddf0cb196322c0b9c58c1ddd6d29a39a087a11ca1b7b43fa95767f9c631e085e57597f2748ef31d0431096ff60a8ed21788b0e1ffbbb9f0f63f015cfa'
            'adbd1cca7b2ae2f64e4ae06351f736e95da65594152a0458cab2e17c63f1c04ab66d23df99e9224af16804c377b466b4a14cda36f9b078541ad134a6c2a1873e'
            '96ecb174a476f64dec3ec086732d93a76d27e9a1cc56b1dc3fa87bf8e00d4b3c44076a01a0d525e42174cacc58fd5c16d5c8af5d4636dae13df549dec8204f7d'
            'f2f8240c18a4483bf3e03b3a5ac8822c3deae713224f732bf28f7fddfcc5d0b02aa91652665ff93f05731e162a9407109c18f43d732a3a9b19d9eacfc806a4df')

prepare() {
  mv shadowsocks-{$_commit,$pkgver}

  cd shadowsocks-$pkgver
  patch -p1 -i ../pycodestyle.patch
  patch -p1 -i ../$pkgname-py3.10-fix-1.patch
  patch -p1 -i ../$pkgname-py3.10-fix-2.patch
}

check() {
  cd shadowsocks-$pkgver
  export SOCKS_CONF=tests/socksify/socks.conf

  # No local http server running, point to a random site
  sed -i "s|default_url = 'http://localhost/'|default_url = 'https://www.gstatic.com/generate_204'|" tests/test.py
  sed -i 's|http://127.0.0.1/|https://www.gstatic.com/generate_204|' tests/jenkins.sh

  sed -i 's|/usr/local/lib/|/usr/lib/|g' tests/jenkins.sh tests/*.json

  tests/jenkins.sh || echo "Tests failed"
}

package() {
  cd shadowsocks-$pkgver

  python setup.py install -O1 --root="$pkgdir"

  install -dm755 "$pkgdir"/usr/share/man/man1
  install -m644 debian/{ssserver.1,sslocal.1} "$pkgdir"/usr/share/man/man1/
  install -Dm644 debian/config.json "$pkgdir"/etc/shadowsocks/example.json

  install -Dm644 "$srcdir"/shadowsocks@.service "$pkgdir"/usr/lib/systemd/system/shadowsocks@.service
  install -Dm644 "$srcdir"/shadowsocks-server@.service "$pkgdir"/usr/lib/systemd/system/shadowsocks-server@.service
}
